#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//PtShape.cpp  - Point Shape Object found in Image      
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Feb 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

#include "PtShapeType.h"
#include "PtShape.h"

#include "ImageDataset.h"

const char * const PtShape::xmlNotes = "Notes";
const char * const PtShape::xmlPtCnt = "PtCnt";

PtShape::PtShape(void)
	: ImageObject('\0')
{
	SetNote( ".");
	_pBoundary = new Boundary();
}

PtShape::PtShape(ImageObjectType *pObjType) :
	ImageObject(pObjType)
{
	SetNote( ".");
	_pBoundary = new Boundary();
}

PtShape::~PtShape(void)
{
	if (_pBoundary)
		delete _pBoundary;
}


bool PtShape::LoadProperties(TiXmlElement *pImageObjectXml)
{
	bool rc = false;

	if (pImageObjectXml != '\0')
	{
		std::string name = pImageObjectXml->GetText();
		SetName(name.c_str());

		SetNote(ImageDataset::GetXmlAttribute(pImageObjectXml, PtShape::xmlNotes, "..."));

		if ( LoadXmlDefinedProperties(pImageObjectXml))
		{
			int cnt = 0;
			if ( pImageObjectXml->Attribute( PtShape::xmlPtCnt, &cnt) != '\0' )
			{
				char buffer[8+1];
				int valX = 0;
				int valY = 0;
				for (int i = 0; i < cnt; i++ )
				{
					if ( _itoa_s( i, &buffer[1], 7, 10 ) != 0 )
						break;
					buffer[0] = 'x';
					if ( pImageObjectXml->Attribute( buffer, &valX) == '\0')
						break;
					else
					{
						buffer[0] = 'y';
						if ( pImageObjectXml->Attribute( buffer, &valY) == '\0')
							break;
						else
						{
							if (i == 0 )
								StartBoundary(valX, valY);
							else
								ExtendBoundary(valX, valY);
						}
					}
				}
				if ( GetBoundarySize() == cnt )
					rc = true;
			}
		}
	}
	return rc;
}


bool	PtShape::SaveProperties(TiXmlElement *pImageObjectXml)
{
	bool rc = false;

	if (pImageObjectXml != '\0' )
	{
		std::string imageObjectTypeName = pImageObjectXml->Attribute("Type");
		if ( imageObjectTypeName ==  GetImageObjectType()->GetName())
		{
			//Notes
			ImageDataset::SetXmlAttribute(pImageObjectXml, PtShape::xmlNotes, GetNote());
	
			if ( SaveXmlDefinedProperties(pImageObjectXml))
			{
				int cnt = GetBoundarySize();
				pImageObjectXml->SetAttribute(PtShape::xmlPtCnt, cnt);				
		
				char buffer[8+1];
				for (int i = 0; i < cnt; i++ )
				{
					if ( _itoa_s( i, &buffer[1], 7, 10 ) != 0 )
					{
						cnt = i;
						break;
					}
					buffer[0] = 'x';
					pImageObjectXml->SetAttribute(buffer, GetBoundaryItemX(i));
					buffer[0] = 'y';
					pImageObjectXml->SetAttribute(buffer, GetBoundaryItemY(i));
				}
				if ( GetBoundarySize() == cnt )
						rc = true;
			}
		}
	}
	return rc;
}



bool PtShape::AssignFrom(ImageObject *pObjectToCopy)
{
	bool rc = false;	
	PtShape *pSrc = dynamic_cast<PtShape *>(pObjectToCopy);

	if(pSrc != '\0' )
	{
		SetImageObjectType(pSrc->GetImageObjectType());
		_pBoundary = pSrc->GetBoundary();
		SetNote(pSrc->GetNote());

		rc = true;
	}
	return rc;
}


//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------